package com.framework.plm.bas.util;

import javax.servlet.ServletContextEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 环境初始化类，以便获取spring容器中的环境信息供系统调用spring中注册的bean
 * 
 * 
 */
public class ContextInit extends ContextLoaderListener {

	protected final static Log logger = LogFactory.getLog(ContextInit.class);

	private static ApplicationContext context;

	public static ApplicationContext getContext() {
		return context;
	}

	public static void setContext(ApplicationContext ctx) {
		context = ctx;
	}

	public void contextInitialized(ServletContextEvent context) {
		System.setProperty("http://apache.org/xml/features/validation/schema",
				"false");

		super.contextInitialized(context);
		/** 设置单例模式的context */
		setContext(WebApplicationContextUtils
				.getRequiredWebApplicationContext(context.getServletContext()));
	}

	/**
	 * 服务器停止时，自动执行方法
	 * @param servlet event
	 */
	public void contextDestroyed(ServletContextEvent servlet) {

		/** 应用平台系统结束 */
		logger.info("应用["+servlet.getServletContext().getServletContextName()+"]关闭，释放资源。");

	}
}
